home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb1 / main.c < prev    next >
C/C++ Source or Header  |  1998-02-01  |  5KB  |  215 lines

  1. #include<dos/rdargs.h>
  2. #include<exec/libraries.h>
  3. #include<workbench/startup.h>
  4. #include<workbench/workbench.h>
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8.  
  9. #include<clib/dos_protos.h>
  10. #include<clib/exec_protos.h>
  11. #include<clib/icon_protos.h>
  12.  
  13. #include "gui.h"
  14. #include "idcmp.h"
  15. #include "loadsave.h"
  16. #include "arexx.h"
  17.  
  18. /* The library base global variables */
  19. /* (The different style of opening libraries requires these to be initialised to NULL) */
  20. struct Library* GfxBase = NULL;
  21. struct Library* IntuitionBase = NULL;
  22. struct Library* GadToolsBase = NULL;
  23. struct Library* AslBase = NULL;
  24. struct Library* DosBase = NULL;
  25. struct Library* IFFBase = NULL;
  26. struct Library* RexxSysBase = NULL;
  27. struct Library* IconBase = NULL;
  28.  
  29. /* Need to give prototypes for our functions */
  30. static int  createAll(struct WBStartup*);
  31. static void freeAll(void);
  32. static int  openLibs(void);
  33. static void closeLibs(void);
  34.  
  35. /* The alternative "main()" for Workbench startup */
  36. void wbmain(struct WBStartup*);
  37.  
  38. /* The shared starting point of our program */
  39. static void realmain(struct WBStartup*);
  40.  
  41. #define TT_DEPTH       "DEPTH"
  42. #define TT_PORTNAME    "PORTNAME"
  43.  
  44. #define ARGS_TEMPLATE  TT_DEPTH "/N," TT_PORTNAME
  45.  
  46. enum ARGS { ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  47.  
  48. #define DEFAULT_PORTNAME "HELLOPAINTER"
  49. #define DEFAULT_DEPTH    (4)
  50.  
  51. /* The CLI starting point for StormC, but the general start for SAS/C */
  52. void main(int argc, char** argv)
  53. {
  54.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  55.   if(argc == 0)
  56.         wbmain((struct WBStartup*)argv);
  57.     else
  58.         realmain(NULL);
  59. }
  60.  
  61. /* The WB starting point for StormC */
  62. void wbmain(struct WBStartup* wbmsg)
  63. {
  64.     /* WB-specific startup could go here */
  65.     realmain(wbmsg);
  66. }
  67.  
  68. /* The start of the program */
  69. static void realmain(struct WBStartup* wbmsg)
  70. {
  71.     if(createAll(wbmsg))
  72.         handleIDCMP();
  73.     freeAll();
  74. }
  75.  
  76. static struct RDArgs* rdargs = NULL;
  77. static struct DiskObject* dobj = NULL;
  78.  
  79. static int createAll(struct WBStartup* wbmsg)
  80. {
  81.   if(openLibs())
  82.     {
  83.         /* We'll only set portname if successful */
  84.         char* portname = NULL;
  85.         UBYTE depth;
  86.         if(wbmsg)
  87.         {
  88.             /* WB bit, use Tool Types */
  89.             BPTR olddir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
  90.             if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
  91.             {
  92.                 UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
  93.                 char* depthptr = FindToolType(tt, TT_DEPTH);
  94.                 portname = FindToolType(tt, TT_PORTNAME);
  95.                 /* Use the default if a Tool Type was not specified */
  96.                 if(portname == NULL)
  97.                     portname = DEFAULT_PORTNAME;
  98.                 if(depthptr)
  99.                     depth = atoi(depthptr);
  100.                 else
  101.                     depth = DEFAULT_DEPTH;
  102.             }
  103.             if(olddir)
  104.                 CurrentDir(olddir);
  105.         }
  106.         else
  107.         {
  108.             /* CLI bit, use ReadArgs() */
  109.             LONG args[NUM_ARGS];
  110.             int i;
  111.             /* Initialise our args to NULL */
  112.             /* (This way we will know if an argument was specified) */
  113.             for(i=0; i<NUM_ARGS; i++)
  114.                 args[i] = NULL;
  115.             if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  116.             {
  117.                 LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  118.                 portname = (char*)(args[ARG_PORTNAME]);
  119.                 /* Use the default if an argument was not specified */
  120.                 if(portname == NULL)
  121.                     portname = DEFAULT_PORTNAME;
  122.                 if(depthptr == NULL)
  123.                     depth = DEFAULT_DEPTH;
  124.                 else
  125.                     depth = (UBYTE)(*depthptr);
  126.             }
  127.             else
  128.                 printf("Error: could not read arguments\n");
  129.         }
  130.         if(portname)
  131.             return createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0);
  132.     }
  133.     return FALSE;
  134. }
  135.  
  136. static void freeAll()
  137. {
  138.     freeReqs();
  139.     closeGUI();
  140.     freeArgs();
  141.     freeARexxPort();
  142.     if(rdargs)
  143.         FreeArgs(rdargs);
  144.     if(dobj)
  145.         FreeDiskObject(dobj);
  146.     closeLibs();
  147. }
  148.  
  149. /* Try to open all the libraries -- return TRUE on success */
  150. static int openLibs()
  151. {
  152.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  153.     {
  154.         printf("Error: could not open graphics.library\n");
  155.         return FALSE;
  156.     }
  157.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  158.     {
  159.         printf("Error: could not open intuition.library\n");
  160.         return FALSE;
  161.     }
  162.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  163.     {
  164.         printf("Error: could not open gadtools.library\n");
  165.         return FALSE;
  166.     }
  167.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  168.     {
  169.         printf("Error: could not open asl.library\n");
  170.         return FALSE;
  171.     }
  172.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  173.     {
  174.         printf("Error: could not open dos.library\n");
  175.         return FALSE;
  176.     }
  177.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  178.     {
  179.         printf("Error: could not open iff.library\n");
  180.         return FALSE;
  181.     }
  182.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  183.     {
  184.         printf("Error: could not open rexxsyslib.library\n");
  185.         return FALSE;
  186.     }
  187.     if((IconBase = OpenLibrary("icon.library",37)) == NULL)
  188.     {
  189.         printf("Error: could not open icon.library\n");
  190.         return FALSE;
  191.     }
  192.   return TRUE;
  193. }
  194.  
  195. /* Close any open library */
  196. static void closeLibs()
  197. {
  198.     if(IconBase)
  199.         CloseLibrary(IconBase);
  200.     if(RexxSysBase)
  201.         CloseLibrary(RexxSysBase);
  202.     if(IFFBase)
  203.         CloseLibrary(IFFBase);
  204.     if(DosBase)
  205.         CloseLibrary(DosBase);
  206.     if(AslBase)
  207.         CloseLibrary(AslBase);
  208.     if(GadToolsBase)
  209.         CloseLibrary(GadToolsBase);
  210.     if(IntuitionBase)
  211.         CloseLibrary(IntuitionBase);
  212.     if(GfxBase)
  213.         CloseLibrary(GfxBase);
  214. }
  215.